package com.example.contactapp.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.example.contactapp.R;
import com.example.contactapp.adapters.ContactAdapter;
import com.example.contactapp.adapters.SectionListAdapter;
import com.example.contactapp.objects.ContactObject;
import com.example.contactapp.providers.ContactThread;
import com.example.contactapp.providers.GetAllContact;
import com.example.contactapp.views.SectionListItem;
import com.example.contactapp.views.SectionListView;

public class ContactFragment extends Fragment implements
		OnItemSelectedListener, OnClickListener {
	private View v;
	private SectionListView list;
	private ContactAdapter mAdapter;
	private ArrayList<SectionListItem> exampleArray, allContacts;
	private SectionListAdapter sectionAdapter;

	private Activity activity;
	private Spinner mySpinerGroup;
	private String mGroupTitle = "";
	private String[] myArrayTitle;
	private Button myBtnAdd;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fragment_contact, null);

		return v;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.activity = activity;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		init();
		getData();
	}


	private void getData() {
		new GetAllContact(activity) {
			protected void onPostExecute(ArrayList<SectionListItem> result) {
				super.onPostExecute(result);
				allContacts = result;
				exampleArray = allContacts;
				mAdapter.addAll(exampleArray);
				mAdapter.notifyDataSetChanged();
				sectionAdapter.notifyDataSetChanged();
			};
		}.execute();
	}

	private void init() {
		exampleArray = new ArrayList<>();
		allContacts = new ArrayList<>();
		list = (SectionListView) v.findViewById(R.id.list_contacts);
		mAdapter = new ContactAdapter(activity, exampleArray){
			@Override
			public void onClick(View arg0) {
				super.onClick(arg0);
				ContactObject ct = (ContactObject) arg0.getTag();
				editContact(ct.getContactUri());
			}
		};
		sectionAdapter = new SectionListAdapter(activity.getLayoutInflater(),
				mAdapter);
		list.setAdapter(sectionAdapter);

		mySpinerGroup = (Spinner) getView().findViewById(R.id.sp_group);
		myArrayTitle = getResources().getStringArray(R.array.arr_group);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
				android.R.layout.simple_spinner_item, myArrayTitle);
		adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		mySpinerGroup.setAdapter(adapter);
		mySpinerGroup.setOnItemSelectedListener(this);

		myBtnAdd = (Button) getView().findViewById(R.id.btn_add);
		myBtnAdd.setOnClickListener(this);
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		mGroupTitle = myArrayTitle[arg2];
		if (arg2 == 0) {
			mAdapter.clear();
			exampleArray = allContacts;
			mAdapter.addAll(exampleArray);
			mAdapter.notifyDataSetChanged();
			sectionAdapter.notifyDataSetChanged();
		} else {
			mAdapter.clear();
			exampleArray = ContactThread.getInstance(activity)
					.getContactsByGroupTitle(mGroupTitle);
			mAdapter.addAll(exampleArray);
			mAdapter.notifyDataSetChanged();
			sectionAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			Intent i = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
			i.putExtra("finishActivityOnSaveCompleted", true);
			startActivityForResult(i, 100);
			break;

		default:
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100) {
			if (resultCode == Activity.RESULT_OK) {
				if (mySpinerGroup.getSelectedItemPosition() != 0)
					mySpinerGroup.setSelection(0);
				getData();
			}
		}
	}

	private void editContact(Uri mSelectedContactUri) {
		
		// Creates a new Intent to edit a contact
		Intent editIntent = new Intent(Intent.ACTION_EDIT);
		/*
		 * Sets the contact URI to edit, and the data type that the Intent must
		 * match
		 */
		editIntent.setDataAndType(mSelectedContactUri,
				Contacts.CONTENT_ITEM_TYPE);

		// Sets the special extended data for navigation
		editIntent.putExtra("finishActivityOnSaveCompleted", true);

		startActivityForResult(editIntent, 100);
	}
}
